<?php

/**
 * 政府监管控制器
 */

namespace Admin\Controller;
class GovermentController extends CommonController{
	/**
	 * 企业用电排行
	 */
	public function Electricity(){
		$count = M('electricity_ranking')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('electricity_ranking')->limit($page->firstRow.','.$page->listRows)->order('elec_number desc')->select();
        $data['status'] = '1';
        $data['level'] = '5';
        $user = M('user')->where($data)->select();
        $this->assign('list', $list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->assign('user',$user);
		$this->display('elec_list');
	}
	/**
     * 查看企业电量图片
     */
    public function elec_photos(){
        $id = I('id');
        $photos = M('electricity_ranking')->field('elec_photo')->where(['id'=>$id])->find();
        $images = json_decode($photos['elec_photo'],true);
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 企业安全排行
     */
    public function safety(){
        $counts = M('safety_ranking')->field('any_value(id) as id,any_value(user_id) as user_id,any_value(company_id) as company_id,any_value(company_name) as company_name,any_value(sum(score)) as score')->group('company_name')->select();
        $count = count($counts);
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('safety_ranking')->field('any_value(id) as id,any_value(user_id) as user_id,any_value(company_id) as company_id,any_value(company_name) as company_name,any_value(sum(score)) as score')->group('company_name')->limit($page->firstRow.','.$page->listRows)->order('score desc')->select();
        $data['status'] = '1';
        $data['level'] = '5';
        $user = M('user')->where($data)->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->assign('user',$user);
        $this->display('safety_list');
    }
    /**
     * 防火巡检分类
     */
    public function fire_item(){
        $count = M('fire_item')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('fire_item')->limit($start,C('PAGE_SIZE'))->order('id desc')->select();
        $data['status'] = '1';
        $data['level'] = '1';
        $user = M('user')->where($data)->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$page);
        $this->assign('user',$user);
        $this->display('fire_item');

    }
    /**
     * 添加防火巡检分类
     */
    public function add_fire_item(){
        if(IS_POST){
            $data['name'] = I('post.name');
            $result = M('fire_item')->add($data);
            if(!empty($data)){
                $this->success('添加成功',U('Goverment/fire_item'));
            }
        }
        $this->display('add_fire_item');
    }
    /**
     * 编辑防火巡检分类
     */
    public function edit_fire_item(){
        if(IS_POST){
            $id = I('id');
            $data['name'] = I('post.name');
            $result = M('fire_item')->where(['id'=>$id])->save($data);
            if(!empty($data)){
                $this->success('修改成功',U('Goverment/fire_item'));
            }
        }
        $id = I('id');
        $info = M('fire_item')->where(['id'=>$id])->find();
        $this->assign('info',$info);
        $this->display('edit_fire_item');
    }
    /**
     * 删除防火巡检分类
     */
    public function del_fire_item(){
        if(IS_AJAX){
            $id = I('id');
            $result = M('fire_item')->where(['id'=>$id])->delete();
            $this->success('删除成功', U('Goverment/fire_item'));
        }
    }
    /**
     * 森林防火巡检记录
     */
    public function get_fire_prevention(){
        $count = M('fire_prevention')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('fire_prevention')->limit($start,C('PAGE_SIZE'))->order('id desc')->select();
        $item = M('fire_item')->select();
        $data['status'] = '1';
        $data['level'] = '1';
        $user = M('user')->where($data)->select();
        $this->assign('list',$list);
        $this->assign('item',$item);
        $this->assign('user',$user);
        $this->assign('count',$count);
        $this->assign('page',$show);    
        $this->display('fire_prevention_list');
    }
    /**
     * 森林防火巡检图片
     */
    public function fire_prevention_photos(){
        $id = I('id');
        $photos = M('fire_prevention')->field('fire_photo')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['fire_photo'].$behind;
        $images = json_decode($json,true);
        // $images = json_decode($photos['report_photo'],true);
        // print_r($images);
        $this->assign('images',$images);
        $this->display();
    }

    /**
     * 删除森林防火巡检记录
     */
    public function del_fire_prevention(){
        if(IS_AJAX){
            $id = I('id');
            $result = M('fire_prevention')->where(['id'=>$id])->delete();
            $this->success('删除成功',U('Goverment/fire_prevention'));
        }
    }
    /**
     * 山林资料
     */
    public function foreast(){
        $count = M('forest_data')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('forest_data')->limit($start,C('PAGE_SIZE'))->order('forest_id desc')->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display('foreast_list');
    }
    /**
     * 添加山林资料
     */
    public function add_foreast(){
        if(IS_POST){
            $data['forest_name'] = I('post.name');
            $data['forest_content'] = I('post.content');
            $data['create_time'] = time();
            if(!empty($data)){
                $result = M('forest_data')->add($data);
                $this->success('添加成功',U('Goverment/foreast'));
            }
        }
        $this->display('add_foreast');
    }
    /**
     * 修改山林资料
     */
    public function edit_foreast(){
        if(IS_POST){
            $id = I('post.id');
            $data['forest_name'] = I('post.name');
            $data['forest_content'] = I('post.content');
            if(!empty($data)){
                $result = M('forest_data')->where(['forest_id'=>$id])->save($data);
                $this->success('修改成功',U('Goverment/foreast'));
            }
        }
        $id = I('forest_id');
        $info = M('forest_data')->where(['forest_id'=>$id])->find();
        $this->assign('info',$info);
        $this->display('edit_foreast');
    }
    /**
     * 删除山林资料
     */
    public function del_foreast(){
        if(IS_AJAX){
            $id = I('forest_id');
            $result = M('forest_data')->where(['forest_id'=>$id])->delete();
            $this->success('删除成功',U('Goverment/foreast'));
        }
    }
    /**
     * 环保巡检分类
     */
    public function environment_item(){
        $count = M('environment_item')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('environment_item')->limit($start,C('PAGE_SIZE'))->order('item_id desc')->select();
        $data['status'] = '1';
        $data['level'] = '1';
        $user = M('user')->where($data)->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$page);
        $this->assign('user',$user);
        $this->display('environment_item_list');
    }
    /**
     * 添加环保巡检分类
     */
    public function add_env_item(){
        if(IS_POST){
            $data['item_name'] = I('item_name');
            if(!empty($data)){
                $result = M('environment_item')->add($data);
                $this->success('添加成功',U('Goverment/environment_item'));
            }
        }
        $this->display('add_env_item');
    }
    /**
     * 编辑环保巡检分类
     */
    public function edit_env_item(){
        if(IS_POST){
            $id = I('item_id');
            $data['item_name'] = I('item_name');
            if(!empty($data)){
                $result = M('environment_item')->where(['item_id'=>$id])->save($data);
                $this->success('修改成功',U('Goverment/environment_item'));
            }
        }
        $id = I('item_id');
        $info = M('environment_item')->where(['item_id'=>$id])->find();
        $this->assign('info',$info);
        $this->display('edit_env_item');
    }
    /**
     * 删除环保巡检分类
     */
    public function del_env_item(){
        if(IS_AJAX){
            $id = I('item_id');
            $result = M('environment_item')->where(['item_id'=>$id])->delete();
            $this->success('删除成功',U('Goverment/environment_item'));
        }
    }
    /**
     * 环保巡检记录
     */
    public function environment_protect(){
        $count = M('environment_protect')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('environment_protect')->limit($start,C('PAGE_SIZE'))->order('id desc')->select();
        $where['status'] = '1';
        $user = M('user')->where($where)->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->assign('user',$user);
        $this->display('env_protect_list');
    }
    /**
     * 环保巡检图片
     */
    public function env_protect_photos(){
        $id = I('id');
        $photos = M('environment_protect')->field('env_photo')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['env_photo'].$behind;
        $images = json_decode($json,true);
        // $images = json_decode($photos['report_photo'],true);
        // print_r($images);
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 删除环保巡检记录
     */
    public function del_env_protect(){
        if(IS_AJAX){
            $id = I('id');
            $result = M('environment_protect')->where(['id'=>$id])->delete();
            $this->success('删除成功',U('environment_protect'));
        }
    }
    /**
     * 环保资料
     */
    public function env_data(){
        $count = M('environment_data')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('environment_data')->limit($start,C('PAGE_SIZE'))->order('pro_id desc')->select(); 
        $this->assign('list',$list);
        $this->display('environment_data_list');
    }
    /**
     * 增加环保资料
     */
    public function add_env_data(){
        if(IS_POST){
            $data['pro_name'] = I('pro_name');
            $data['pro_content'] = I('pro_content');
            if(!empty($data)){
                $result = M('environment_data')->add($data);
                $this->success('添加成功',U('env_data'));
            }
        }
        $this->display('add_env_data');
    }
    /**
     * 修改环保资料
     */
    public function edit_env_data(){
        if(IS_POST){
            $id = I('pro_id');
            $data['pro_name'] = I('pro_name');
            $data['pro_content'] = I('pro_content');
            if(!empty($data)){
                $result = M('environment_data')->where(['pro_id'=>$id])->save($data);
                $this->success('修改成功',U('env_data'));
            }
        }
        $id = I('pro_id');
        $info = M('environment_data')->where(['pro_id'=>$id])->find();
        $this->assign('info',$info);
        $this->display('edit_env_data');
    }
    /**
     * 删除环保资料
     */
    public function del_env_data(){
        if(IS_AJAX){
            $id = I('pro_id');
            $result = M('environment_data')->where(['pro_id'=>$id])->delete();
            $this->success('删除成功',U('env_data'));
        }
    }
}